package org.examples;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicLong;

import org.apache.hc.core5.concurrent.FutureCallback;
import org.apache.hc.core5.http.EntityDetails;
import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.HttpException;
import org.apache.hc.core5.http.HttpResponse;
import org.apache.hc.core5.http.nio.AsyncResponseConsumer;
import org.apache.hc.core5.http.nio.CapacityChannel;
import org.apache.hc.core5.http.protocol.HttpContext;
import org.synchronoss.cloud.nio.multipart.Multipart;
import org.synchronoss.cloud.nio.multipart.MultipartContext;
import org.synchronoss.cloud.nio.multipart.NioMultipartParser;
import org.synchronoss.cloud.nio.multipart.NioMultipartParserListener;
import org.synchronoss.cloud.nio.stream.storage.StreamStorage;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class VocsMultiPartResponseConsumer implements AsyncResponseConsumer {
  final CountDownLatch latch;

  final NioMultipartParser parser;

  AtomicLong startMs;

  Path audioPath = Paths.get("audio.opus");

  public VocsMultiPartResponseConsumer(CountDownLatch latch, AtomicLong startMs, NioMultipartParser parser) {
    this.latch = latch;
    this.parser = parser;
    this.startMs = startMs;
    try {
      if (audioPath.toFile().exists()) {
        audioPath.toFile().delete();
      }
      audioPath.toFile().createNewFile();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  @Override
  public void consumeResponse(HttpResponse httpResponse, EntityDetails entityDetails, HttpContext httpContext,
      FutureCallback futureCallback) throws HttpException, IOException {

  }

  @Override
  public void informationResponse(HttpResponse httpResponse, HttpContext httpContext) throws HttpException, IOException {

  }

  @Override
  public void failed(Exception e) {

  }

  @Override
  public void updateCapacity(CapacityChannel capacityChannel) throws IOException {
    capacityChannel.update(Integer.MAX_VALUE);

  }

  @Override
  public void consume(ByteBuffer byteBuffer) throws IOException {
    log.info("------------consume:{}", System.currentTimeMillis() - startMs.get());
    //    int len=byteBuffer.capacity();
    int len = byteBuffer.remaining();
    byte[] bytes = new byte[len];

    // transfer bytes from this buffer into the given destination array
    byteBuffer.get(bytes, 0, len);
    log.info("parser.write--->");
    parser.write(bytes, 0, len);
    log.info("<---parser.write");
    //    String bodyString = StandardCharsets.UTF_8.decode(byteBuffer).toString();
    //    log.info(bodyString);
  }

  @Override
  public void streamEnd(List<? extends Header> list) throws HttpException, IOException {

  }

  @Override
  public void releaseResources() {
    latch.countDown();
    long fileLength = audioPath.toFile().length();
    log.info(String.valueOf(fileLength));
  }
}
//23726